/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.administrador.control;

import DBMS.DataBaseManager;
import clases.Email;
import clases.Periodo;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author charles
 */
public class ModificarEmail extends DispatchAction {

    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Email p = (Email) form;
        DataBaseManager dbms = DataBaseManager.getInstance();
        boolean modificado = dbms.modificarEmail(p);
        if (modificado) {
            p.setMensaje("Email modificado exitosamente");
            return mapping.findForward("success");
        } else {
            p.setMensaje("Problemas al modificar el email");
            return mapping.findForward("success");
        }
    }

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager dbms = DataBaseManager.getInstance();
        Email p = new Email();
        Email q = (Email) form;
        List list = dbms.consultarEmail(p);
        //p = (list != null && !list.isEmpty() ? (Email) list.get(0) : null);
        if (!list.isEmpty())
            p = (Email) list.get(0);
        else
            p = null;
        if (p == null) {
            q.setMensaje("No se pudo consultar el email");
            return mapping.findForward("failure");
        } else {
            q.cloneWith(p);
            System.out.println(q.getCorreoRemitente());
            return mapping.findForward("success");

        }
    }
}